//
//  HBMeController.m
//  HeartBeat
//
//  Created by karong on 2023/9/1.
//

#import "HBMeController.h"
#import "AppDelegate.h"

#import "HBMeInfoCell.h"
#import "HBMeCell.h"
#import "HBAppLanguage.h"

@interface HBMeController () <GRMineUserCellDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@end

@implementation HBMeController

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.hidesBottomBarWhenPushed = NO;
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.listView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorFromHexRGB:@"F7F7F7"];
}

- (void)zqsbAction {
    [ViewManager pushTaskVCInController:self];
}

- (void)xxzxAction {
    [ViewManager pushWebVCWithUrl:API_HEARTBEAT_FEEDBACK_LIST titleStr:@"" inController:self];
}

- (void)yjfkAction {
    [ViewManager pushFeedbackVCInController:self];
}

- (void)yyqhAction {
    [ViewManager showLanguageWithSelectHandle:^(NSInteger integer) {
        [HBAppLanguage setUserLanguageType:integer];
    } inController:self];
}

- (void)szAction {
    [ViewManager pushSettingVCInController:self];
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = self.dataArray.count;
    return count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell * cell;
    if (indexPath.row == 0) {
        HBMeInfoCell * meInfoCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBMeInfoCell" forIndexPath:indexPath];
        meInfoCell.delegate = self;
        [meInfoCell setDataWithObject:appUserCenter];
        cell = meInfoCell;
    }
    else {
        HBMeCell * meCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HBMeCell" forIndexPath:indexPath];
        [meCell setDataWithTitle:self.dataArray[indexPath.row][@"title"] imageStr:self.dataArray[indexPath.row][@"image"]];
        cell = meCell;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = HBScreenWidth;
    if (indexPath.row == 0) {
        size.height = 60 + 19.5 + ((appUserCenter.vip_status == 1)?0:61.5) + 81 + 12;
    }
    else {
        size.height = 70;
    }
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary * dic = self.dataArray[indexPath.row];
    SEL sel = NSSelectorFromString(dic[@"sel"]);
    if ([self respondsToSelector:sel]) {
        [self performSelector:sel withObject:nil];
    }
}
#pragma mark GRMineUserCellDelegate
- (void)mineUserCellUserAction {
    if (appUserCenter.isLogin) {
        [ViewManager pushUserInfoVCWithUser:appUserCenter inController:self];
    }
    else {
        [((AppDelegate*)([UIApplication sharedApplication].delegate)) initMainPage];
    }
}

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@{@"title":@"",
                         @"image":@"",
                         @"sel":@""},
                       @{@"title":NSLocalizedString(@"賺取書幣",@"123123123"),
                         @"image":@"icon_zqsb",
                         @"sel":@"zqsbAction"},
                       @{@"title":WSLocalized(@"消息中心"),
                         @"image":@"icon_xxzx",
                         @"sel":@"xxzxAction"},
                       @{@"title":WSLocalized(@"意見反饋"),
                         @"image":@"icon_yjfk",
                         @"sel":@"yjfkAction"},
                       @{@"title":WSLocalized(@"語言切換"),
                         @"image":@"icon_yyqh",
                         @"sel":@"yyqhAction"},
                       @{@"title":WSLocalized(@"設置"),
                         @"image":@"icon_sz",
                         @"sel":@"szAction"}];
    }
    return _dataArray;
}
@end
